#import "CustomTableViewCell.h"

@implementation CustomTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
        [self setupConstraints];
    }
    return self;
}

- (void)setupUI {
    // 图标
    self.iconImageView = [[UIImageView alloc] init];
    self.iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.iconImageView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.iconImageView];
    
    // 标题标签
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    self.titleLabel.textColor = [UIColor darkTextColor];
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.titleLabel];
    
    // 操作按钮
    self.actionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.actionButton setTitle:@"操作" forState:UIControlStateNormal];
    [self.actionButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    self.actionButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.actionButton];
//    self.contentView.backgroundColor = [UIColor clearColor];
}

- (void)setupConstraints {
    // 使用 Auto Layout 设置约束
    [NSLayoutConstraint activateConstraints:@[
        // 图标约束
        [self.iconImageView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:15],
        [self.iconImageView.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor],
        [self.iconImageView.widthAnchor constraintEqualToConstant:30],
        [self.iconImageView.heightAnchor constraintEqualToConstant:30],
        
        // 标题标签约束
        [self.titleLabel.leadingAnchor constraintEqualToAnchor:self.iconImageView.trailingAnchor constant:15],
        [self.titleLabel.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor],
        [self.titleLabel.trailingAnchor constraintEqualToAnchor:self.actionButton.leadingAnchor constant:-10],
        
        // 按钮约束
        [self.actionButton.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-15],
        [self.actionButton.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor],
        [self.actionButton.widthAnchor constraintEqualToConstant:60],
        [self.actionButton.heightAnchor constraintEqualToConstant:35]
    ]];
}

- (void)buttonTapped:(UIButton *)sender {
    // 通过代理传递按钮点击事件
    if ([self.delegate respondsToSelector:@selector(customCell:didTapButton:)]) {
        [self.delegate customCell:self didTapButton:sender];
    }
}

- (void)configureWithImage:(UIImage *)image title:(NSString *)title buttonTitle:(NSString *)buttonTitle {
    self.iconImageView.image = image;
    self.titleLabel.text = title;
    [self.actionButton setTitle:buttonTitle forState:UIControlStateNormal];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    self.iconImageView.image = nil;
    self.titleLabel.text = nil;
    [self.actionButton setTitle:@"操作" forState:UIControlStateNormal];
    self.delegate = nil;
}

@end
